/**
 * 动态日期解析类
 * import resolveDyncDate from 'mars-pro/lib/pro/resolve-dyncdate'
 * 1. 参数
 * //静态日期
 * resolveDyncDate('2022-08-05')
 * // 当天 
 * resolveDyncDate('day')
 * // 前10天
 * resolveDyncDate('day-10')
 * // 后10天 
 * resolveDyncDate('day+10')
 * //当周当天
 * resolveDyncDate('week')
 * //上一周的当天
 * resolveDyncDate('week-1')
 * //上一周的第1天
 * resolveDyncDate('week-1:1')
 * //上一周当天的前1天
 * resolveDyncDate('week-1:-1')
  * //上一周当天的后1天
 * resolveDyncDate('week-1:+1')
 * 
 * // 以下语法和week相同
 * resolveDyncDate('month')
 * resolveDyncDate('year')
 * 
 * @author xiufu.wang
 */
import moment from 'moment'
import localeZh from 'moment/locale/zh-cn.js'
moment.updateLocale(localeZh)
class ResilveDateSyntax {
    static create(syntax) {
        return new ResilveDateSyntax(syntax)
    }
    constructor(syntax, option) {
        this.syntax = syntax
        this.option = option
    }
    resolve() {
        return (
            this.resolveDay() ||
            this.resolveWeek() ||
            this.resolveMonth() ||
            this.resolveYear() ||
            this.resolveStatic()
        )
    }
    resolveStatic() {
        let r = moment(this.syntax)
        if (this.syntax && r.isValid()) {
            return r._d
        }
        return undefined
    }
    /**
     * day 当天
     * day- n 前n天
     * day + n 后n天
     * @returns 
     */
    resolveDay() {
        const syntax = this.syntax
        if (syntax && syntax.slice(0, 3) === 'day') {
            if (syntax === 'day') {
                return moment()._d
            }
            //-
            let arg = syntax.slice(4)
            if (syntax[3] === '-' && arg) {
                if (!isNaN(arg)) {
                    return moment().add(-1 * (+arg), 'days')._d
                }
            }
            //+
            if (syntax[3] === '+' && arg) {
                if (!isNaN(arg)) {
                    return moment().add((+arg), 'days')._d
                }
            }
        }
        return undefined;
    }

    /**
     * week 本周当天
     * week-n 前n周 当天
     * week+n 后n周 当天
     * week-n:0 上n周的当天
     * week-n:1 上n周的当1天
     * week-n:-1 上n周的当天的前一天
     * week-n:+1 上n周的当天的后一天
     * week-n:7 上n周的最后一天
     * @returns 
     */
    resolveWeek() {
        const syntax = this.syntax
        let nweek;
        let nday
        // 是否参考当天
        let isReDay = false
        if (syntax && syntax.slice(0, 4) === 'week') {
            const sep = syntax.split(':')
            //本周
            if (syntax === 'week') {
                nweek = 0
                isReDay = true
            }
            //前n周
            if (sep[0].slice(0, 5) === 'week-') {
                let x = sep[0].slice(5)
                if (x && !isNaN(x)) {
                    nweek = -1 * (+x)
                }
            }
            // 后n周
            if (sep[0].slice(0, 5) === 'week+') {
                let x = sep[0].slice(5)
                if (x && !isNaN(x)) {
                    nweek = (+x)
                }
            }

            if (!sep[1]) {
                isReDay = true
                nday = 0
            } else {
                if (sep[1][0] !== '+' && sep[1][0] !== '-') {
                    nday = +(sep[1])
                } else {
                    isReDay = true
                    nday = +(sep[1])
                }
            }
            // 获取周信息
            let nweekValue = moment().add(nweek, 'weeks')
            // 相对于
            if (isReDay) {
                nweekValue = nweekValue.add(nday, 'days')
            } else {
                if (nday > 0) {
                    nweekValue = nweekValue.startOf('weeks')
                    if (nday >= 7) {
                        nweekValue = nweekValue.endOf('weeks')
                    } else {
                        nweekValue = nweekValue.add(nday - 1, 'days')
                    }
                }
            }
            return nweekValue._d
        }
        return undefined;
    }
    /**
     * month 本月当天
     * month-n 前n月当天
     * month+n 后n月当天
     * month-n:0 上n月的当天
     * month-n:1 上n月的当1天
     * month-n:-1 上n月的当天的前一天
     * month-n:+1 上n月的当天的后一天
     * month-n:31 上n月的最后一天
     * @returns 
     */
    resolveMonth() {
        const syntax = this.syntax
        let nmonth;
        let nday
        // 是否参考当天
        let isReDay = false
        if (syntax && syntax.slice(0, 5) === 'month') {
            const sep = syntax.split(':')
            //本周
            if (syntax === 'month') {
                nmonth = 0
                isReDay = true
            }
            //前n周
            if (sep[0].slice(0, 6) === 'month-') {
                let x = sep[0].slice(6)
                if (x && !isNaN(x)) {
                    nmonth = -1 * (+x)
                }
            }
            // 后n周
            if (sep[0].slice(0, 6) === 'month+') {
                let x = sep[0].slice(6)
                if (x && !isNaN(x)) {
                    nmonth = (+x)
                }
            }

            if (!sep[1]) {
                isReDay = true
                nday = 0
            } else {
                if (sep[1][0] !== '+' && sep[1][0] !== '-') {
                    nday = +(sep[1])
                } else {
                    isReDay = true
                    nday = +(sep[1])
                }
            }
            // 获取周信息
            let nmonthValue = moment().add(nmonth, 'months')
            // 相对于
            if (isReDay) {
                nmonthValue = nmonthValue.add(nday, 'days')
            } else {
                if (nday > 0) {
                    nmonthValue = nmonthValue.startOf('months')
                    if (nday >= 31) {
                        nmonthValue = nmonthValue.endOf('months')
                    } else {
                        nmonthValue = nmonthValue.add(nday - 1, 'days')
                    }
                }
            }
            return nmonthValue._d
        }
        return undefined;
    }

    /**
     * year 本年当天
     * year-n 前n年当天
     * year+n 后n年当天
     * year-n:0 上n年的当天
     * year-n:1 上n年的当1天
     * year-n:-1 上n年的当天的前一天
     * year-n:+1 上n年的当天的后一天
     * year-n:365 上n年的最后一天
     * @returns 
     */
    resolveYear() {
        const syntax = this.syntax
        let nyear;
        let nday
        // 是否参考当天
        let isReDay = false
        if (syntax && syntax.slice(0, 4) === 'year') {
            const sep = syntax.split(':')
            //本周
            if (syntax === 'year') {
                nyear = 0
                isReDay = true
            }
            //前n周
            if (sep[0].slice(0, 5) === 'year-') {
                let x = sep[0].slice(5)
                if (x && !isNaN(x)) {
                    nyear = -1 * (+x)
                }
            }
            // 后n周
            if (sep[0].slice(0, 5) === 'year+') {
                let x = sep[0].slice(5)
                if (x && !isNaN(x)) {
                    nyear = (+x)
                }
            }

            if (!sep[1]) {
                isReDay = true
                nday = 0
            } else {
                if (sep[1][0] !== '+' && sep[1][0] !== '-') {
                    nday = +(sep[1])
                } else {
                    isReDay = true
                    nday = +(sep[1])
                }
            }
            // 获取周信息
            let nyearValue = moment().add(nyear, 'years')
            // 相对于
            if (isReDay) {
                nyearValue = nyearValue.add(nday, 'days')
            } else {
                if (nday > 0) {
                    nyearValue = nyearValue.startOf('years')
                    if (nday >= 365) {
                        nyearValue = nyearValue.endOf('years')
                    } else {
                        nyearValue = nyearValue.add(nday - 1, 'days')
                    }
                }
            }
            return nyearValue._d
        }
        return undefined;
    }
}

export default function resolveDyncDate(syntax) {
    return ResilveDateSyntax.create(syntax).resolve()
}

export {
    moment
}